package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFactoryDataMapper;
import com.csun.dsms.model.huang.HgFactoryData;
import com.csun.dsms.model.huang.HgFixedAssetData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgFactoryDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgFactoryDataServiceImpl extends BaseService<HgFactoryData>
        implements HgFactoryDataService {

    @Resource
    private HgFactoryDataMapper hgFactoryDataMapper;
    
    @Override
    public List<HgFactoryData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgFactoryDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgFactoryDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importFactoryData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFactoryData hgFactoryData = new HgFactoryData();
                hgFactoryData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgFactoryData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgFactoryData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgFactoryData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgFactoryData.setTotal(Convert.toDouble(excelMap.get("总产值")));
                hgFactoryData.setGrowthTotal(Convert.toDouble(excelMap.get("增加值")));
                hgFactoryData.setGrowthRate(Convert.toDouble(excelMap.get("增加值增速")));
                hgFactoryData.setTopRate(Convert.toDouble(excelMap.get("上一级增速")));
                hgFactoryData.setGrowthRateRanking(Convert.toInt(excelMap.get("增长率排名")));
                hgFactoryData.setLastRanking(Convert.toInt(excelMap.get("环比比的去年的排名")));
                hgFactoryData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgFactoryData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgFactoryData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgFactoryData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgFactoryData.setCreatedTime(new Date());
                hgFactoryDataMapper.insert(hgFactoryData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入工业的数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
